{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font size=6.5 color=\"blue\">** Recommendations with pandas **</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 相似度计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "critics={\n",
    "    'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5,'Just My Luck': 3.0, 'Superman Returns': 3.5, \n",
    "                  'You, Me and Dupree': 2.5, 'The Night Listener': 3.0},\n",
    "    'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5, \n",
    "                     'Just My Luck': 1.5, 'Superman Returns': 5.0, 'The Night Listener': 3.0, 'You, Me and Dupree': 3.5}, \n",
    "    'Michael Phillips': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0,'Superman Returns': 3.5, 'The Night Listener': 4.0},\n",
    "    'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0,'The Night Listener': 4.5, 'Superman Returns': 4.0, 'You, Me and Dupree': 2.5},\n",
    "    'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0, 'Just My Luck': 2.0, 'Superman Returns': 3.0, 'The Night Listener': 3.0,\n",
    "                     'You, Me and Dupree': 2.0}, \n",
    "    'Jack Matthews': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,'The Night Listener': 3.0, 'Superman Returns': 5.0, \n",
    "                      'You, Me and Dupree': 3.5},\n",
    "    'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}} "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "critics = pd.DataFrame(critics)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Claudia Puig</th>\n",
       "      <th>Gene Seymour</th>\n",
       "      <th>Jack Matthews</th>\n",
       "      <th>Lisa Rose</th>\n",
       "      <th>Michael Phillips</th>\n",
       "      <th>Mick LaSalle</th>\n",
       "      <th>Toby</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Just My Luck</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1.5</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Lady in the Water</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.5</td>\n",
       "      <td>2.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Snakes on a Plane</th>\n",
       "      <td>3.5</td>\n",
       "      <td>3.5</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Superman Returns</th>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Night Listener</th>\n",
       "      <td>4.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>You, Me and Dupree</th>\n",
       "      <td>2.5</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.5</td>\n",
       "      <td>2.5</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    Claudia Puig  Gene Seymour  Jack Matthews  Lisa Rose  \\\n",
       "Just My Luck                 3.0           1.5            NaN        3.0   \n",
       "Lady in the Water            NaN           3.0            3.0        2.5   \n",
       "Snakes on a Plane            3.5           3.5            4.0        3.5   \n",
       "Superman Returns             4.0           5.0            5.0        3.5   \n",
       "The Night Listener           4.5           3.0            3.0        3.0   \n",
       "You, Me and Dupree           2.5           3.5            3.5        2.5   \n",
       "\n",
       "                    Michael Phillips  Mick LaSalle  Toby  \n",
       "Just My Luck                     NaN           2.0   NaN  \n",
       "Lady in the Water                2.5           3.0   NaN  \n",
       "Snakes on a Plane                3.0           4.0   4.5  \n",
       "Superman Returns                 3.5           3.0   4.0  \n",
       "The Night Listener               4.0           3.0   NaN  \n",
       "You, Me and Dupree               NaN           2.0   1.0  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "critics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from scipy.stats import pearsonr\n",
    "from scipy.spatial.distance import euclidean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "code_folding": [
     0,
     10
    ],
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def sim_pearson(prefs,p1,p2):\n",
    "    '''输入一个dataFrame，及任意两列的名字'''\n",
    "    df1=prefs[[p1,p2]]\n",
    "    df2=df1[(df1[p1]>=0) & (df1[p2]>=0)]\n",
    "    \n",
    "    r,p_value=pearsonr(df2.iloc[:,0],df2.iloc[:,1])\n",
    "    \n",
    "    if p_value==1: return 0\n",
    "    return r\n",
    "\n",
    "def sim_distance(prefs,p1,p2):\n",
    "    df1=prefs[[p1,p2]]\n",
    "    df2=df1[(df1[p1]>=0) & (df1[p2]>=0)]\n",
    "    d=euclidean(df2.iloc[:,0],df2.iloc[:,1])\n",
    "    return 1/(1+d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "code_folding": [
     0
    ]
   },
   "outputs": [],
   "source": [
    "def topMatches(prefs,p1,n=5,similarity=sim_pearson):\n",
    "    '''返回一个：p1与其他元素的相关度的DataFrame'''\n",
    "    \n",
    "    scores=[(other, similarity(prefs,p1, other)) for other in prefs.columns if other !=p1 ]\n",
    "    scores=pd.DataFrame(scores, columns=['Name', 'Score'])\n",
    "    scores=scores.sort_values(by='Score', ascending=False)\n",
    "    return scores[:n]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.29429805508554946"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim_distance(critics,'Lisa Rose', 'Gene Seymour')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 推荐物品\n",
    "- 输入“人”\n",
    "- 输出对未看过电影的评分预测排名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def getRecommendattions(prefs,p1,similarity=sim_pearson):\n",
    "    df1=prefs[p1][np.isnan(prefs[p1])]\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Lisa Rose</td>\n",
       "      <td>0.991241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Mick LaSalle</td>\n",
       "      <td>0.924473</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Claudia Puig</td>\n",
       "      <td>0.893405</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Jack Matthews</td>\n",
       "      <td>0.662849</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Gene Seymour</td>\n",
       "      <td>0.381246</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Michael Phillips</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               Name     Score\n",
       "3         Lisa Rose  0.991241\n",
       "5      Mick LaSalle  0.924473\n",
       "0      Claudia Puig  0.893405\n",
       "2     Jack Matthews  0.662849\n",
       "1      Gene Seymour  0.381246\n",
       "4  Michael Phillips -1.000000"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r=topMatches(critics,'Toby',n=len(critics.columns)-1)\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Just My Luck          NaN\n",
       "Lady in the Water     NaN\n",
       "Snakes on a Plane     4.5\n",
       "Superman Returns      4.0\n",
       "The Night Listener    NaN\n",
       "You, Me and Dupree    1.0\n",
       "Name: Toby, dtype: float64"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "critics['Toby']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Claudia Puig</th>\n",
       "      <th>Gene Seymour</th>\n",
       "      <th>Jack Matthews</th>\n",
       "      <th>Lisa Rose</th>\n",
       "      <th>Michael Phillips</th>\n",
       "      <th>Mick LaSalle</th>\n",
       "      <th>Toby</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Score</th>\n",
       "      <td>0.893405</td>\n",
       "      <td>0.381246</td>\n",
       "      <td>0.662849</td>\n",
       "      <td>0.991241</td>\n",
       "      <td>-1</td>\n",
       "      <td>0.924473</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Claudia Puig Gene Seymour Jack Matthews Lisa Rose Michael Phillips  \\\n",
       "Score     0.893405     0.381246      0.662849  0.991241               -1   \n",
       "\n",
       "      Mick LaSalle  Toby  \n",
       "Score     0.924473   NaN  "
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rr=r.T.iloc[1:,:].rename(columns=r.iloc[:,0]).reindex(columns=critics.columns)\n",
    "rr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[nan, 8.345183611007972, nan, 9.375534475390111, nan,\n",
       "        9.461808577589977, nan]], dtype=object)"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(rr,critics)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {
    "height": "679px",
    "left": "0px",
    "right": "1388px",
    "top": "107px",
    "width": "212px"
   },
   "toc_section_display": "block",
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
